/*
# Copyright (c) 2024 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
*/

#ifndef OHOS_PJSIP_PROJECT_OHOS_VIDEO_RECEIVER_H
#define OHOS_PJSIP_PROJECT_OHOS_VIDEO_RECEIVER_H

#include <cstdint>
#include <map>
#include <memory>
#include <multimedia/image_framework/image/image_receiver_native.h>
#include <ohcamera/camera.h>
#include <ohcamera/camera_input.h>
#include <ohcamera/capture_session.h>
#include <ohcamera/preview_output.h>

typedef struct FrameCallBack {
    void (*onFrameCallBack)(void *steam, void *buff, int length);
} FrameCallBack;

class VideoFrameReceiver {
public:
    static VideoFrameReceiver* Create(int32_t width, int32_t height, FrameCallBack observer);
    VideoFrameReceiver(int32_t width, int32_t height, FrameCallBack observer);
    ~VideoFrameReceiver();
    int StartCamera();
    void StopCamera();
    int getCameraCount();
    void destroy();
    Camera_Device getCamera(int index);
    Camera_Profile** getPreviewProfileByIndex(int index,uint32_t* size);
    void setWidth(int width) { width_ = width; }
    void setHeight(int height) { height_ = height; }
    void setCameraIndex(int index) { camera_dev_index_ = index; }
    void setPjsipStream(void *s) { steam = s; }

protected:
    static void OnImageReceiverCallback_1(OH_ImageReceiverNative *receiver);
    void OnImageReceiverCallback();
    void InitCamera(int32_t width, int32_t height);
    void PreviewOutputCreate();
    void CameraInputCreate();
    void CaptureSessionSetting();
    void CreateVideoFrameReceiver();

public:
    Camera_Profile *preview_profile_{nullptr};


private:
    int width_ {0};
    int height_ {0};
    int format_ {0};
    uint64_t surfaceId_ {0};
    FrameCallBack callback_;
    bool is_camera_started_{false};
    OH_ImageReceiverNative *receiver_{};
    Camera_Manager *camera_manager_{nullptr};
    Camera_Device *cameras_{nullptr};
    uint32_t cameras_size_{0};
    uint32_t camera_dev_index_{1};
    uint32_t profile_index_{0};
    void *steam{nullptr};
    Camera_PreviewOutput *preview_output_{nullptr};
    Camera_Input *camera_input_{nullptr};
    Camera_CaptureSession *capture_session_{nullptr};

protected:
    static std::map<OH_ImageReceiverNative *, VideoFrameReceiver *> receiverMap_;
};

#endif // OHOS_PJSIP_PROJECT_OHOS_VIDEO_RECEIVER_H